﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class MachineStaticRepo
    {
        public const string m_msg_head = "MachineStatic";

        public IEnumerable<MachineStaticModels> GetMachineStatic()
        {
            List<MachineStaticModels> MachineStatics = new List<MachineStaticModels>();
            string SQLCommand = @"select * from TBMachineStatic";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        MachineStaticModels MachineStatic = new MachineStaticModels();
                        MachineStatic.MachineId = (string)reader["MachineId"];
                        MachineStatic.LocationId = (string)reader["LocationId"];
                        MachineStatic.Meter = (double)reader["Meter"];
                        MachineStatic.Status = (string)reader["Status"];
                        MachineStatic.CreateBy = (string)reader["CreateBy"];
                        MachineStatic.CreateOn = (DateTime)reader["CreateOn"];
                        MachineStatic.UpdateBy = (string)reader["UpdateBy"];
                        MachineStatic.UpdateOn = (DateTime)reader["UpdateOn"];

                        MachineStatics.Add(MachineStatic);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return MachineStatics.ToList();
        }

        public MachineStaticModels GetMachineStaticById(string MachineId)
        {
            MachineStaticModels MachineStatic = new MachineStaticModels();
            string SQLCommand = @"select * from TBMachineStatic where MachineId=@MachineId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@MachineId", MachineId);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        MachineStatic.MachineId = (string)reader["MachineId"];
                        MachineStatic.LocationId = (string)reader["LocationId"];
                        MachineStatic.Meter = (double)reader["ProcedureRemarkEng"];
                        MachineStatic.Status = (string)reader["Status"];
                        MachineStatic.CreateBy = (string)reader["CreateBy"];
                        MachineStatic.CreateOn = (DateTime)reader["CreateOn"];
                        MachineStatic.UpdateBy = (string)reader["UpdateBy"];
                        MachineStatic.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return MachineStatic;
        }

        public bool Create(MachineStaticModels MachineStatic)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBMachineStatic 
                                    values (@MachineId,@LocationId,@Meter,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@MachineId", "GENCODE");
                    db.AddParameter("@LocationId", MachineStatic.LocationId);
                    db.AddParameter("@Meter", MachineStatic.Meter);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID", "900153");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(MachineStaticModels MachineStatic)
        {
            bool result = true;
            string SQLCommand = @"Update TBProcedureRemark 
                                    Set ProcedureRemarkTh=@ProcedureRemarkTh,
                                        ProcedureRemarkEng=@ProcedureRemarkEng,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@MachineId", MachineStatic.MachineId);
                    db.AddParameter("@LocationId", MachineStatic.LocationId);
                    db.AddParameter("@Meter", MachineStatic.Meter);
                    db.AddParameter("@UserID", "900153");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(MachineStaticModels MachineStatic)
        {
            bool result = true;
            string SQLCommand = @"Update TBMachineStatic 
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where MachineId=@MachineId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@MachineId", MachineStatic.MachineId);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID", "900153");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}